package com.clx.mx.xjar.common;

import java.io.*;

public class FileUtil {

    public static byte[] readByte(InputStream is) {
        ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
        try {
            int len;
            byte[] temp = new byte[1024];
            while ((len = is.read(temp)) != -1) {
                outBuffer.write(temp, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }

        return outBuffer.toByteArray();
    }

    /**
     * 读取文件
     *
     * @param file
     * @return
     */
    public static byte[] readByte(File file) {
        if (file == null && !file.exists()) {
            return null;
        }

        InputStream is = null;
        ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
        try {
            int len;
            byte[] temp = new byte[1024];
            is = new FileInputStream(file);
            while ((len = is.read(temp)) != -1) {
                outBuffer.write(temp, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }

        return outBuffer.toByteArray();
    }


    /**
     * 将数据写入文件
     *
     * @param file  文件路径
     * @param bytes 文件内容(如果内容空,则只创建文件)
     * @return
     */
    public static boolean writeByte(File file, byte[] bytes) {
        if (file == null) {
            return false;
        }

        //文件中无内容
        if (bytes == null) {
            bytes = new byte[0];
        }

        OutputStream out = null;
        try {
            out = new FileOutputStream(file);
            out.write(bytes);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static int copyFile(File srcFile, File desFile, boolean isOver) {
        if (!srcFile.exists()) {
            return -2;
        }

        Boolean writeFlag = true;
        if (desFile.exists()) {
            if (isOver) {
                desFile.delete();
            } else {
                writeFlag = false;
            }
        }

        if (writeFlag) {
            try {
                byte[] bytes = FileUtil.readByte(srcFile);
                FileUtil.writeByte(desFile, bytes);
            } catch (Exception e) {
                e.printStackTrace();
                return -1;
            }
        }
        return 1;
    }

    public static boolean mkdir(File webFilePath) {
        if (!webFilePath.exists()) {
            File parentFile = webFilePath.getParentFile();
            parentFile.mkdirs();
            return true;
        } else {
            return true;
        }
    }
}
